/**
 * customerlog服务实现
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.service.impl;

import com.sxf.crm.dto.CustomerLogDTO;
import com.sxf.crm.entity.CustomerLog;
import com.sxf.crm.entity.Customer;
import com.sxf.crm.entity.User;
import com.sxf.crm.entity.Department;
import com.sxf.crm.entity.Contact;
import com.sxf.crm.enums.CustomerLogOperationType;
import com.sxf.crm.repository.CustomerLogRepository;
import com.sxf.crm.service.CustomerLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Transactional
public class CustomerLogServiceImpl implements CustomerLogService {
    
    private final CustomerLogRepository customerLogRepository;
    
    @Override
    public CustomerLog logCreate(Customer customer, User operator) {
        CustomerLog log = new CustomerLog();
        log.setCustomer(customer);
        log.setOperator(operator);
        log.setOperationType(CustomerLogOperationType.CREATE);
        log.setOperationTime(System.currentTimeMillis());
        log.setOperationDesc("创建客户");
        log.setStatusAfter(customer.getStatus().name());
        return customerLogRepository.save(log);
    }
    
    @Override
    public CustomerLog logUpdate(Customer customer, User operator, String details) {
        CustomerLog log = new CustomerLog();
        log.setCustomer(customer);
        log.setOperator(operator);
        log.setOperationType(CustomerLogOperationType.UPDATE);
        log.setOperationTime(System.currentTimeMillis());
        log.setOperationDesc("更新客户信息");
        log.setDetails(details);
        return customerLogRepository.save(log);
    }
    
    @Override
    public CustomerLog logStatusChange(Customer customer, User operator, String statusBefore, String statusAfter) {
        CustomerLog log = new CustomerLog();
        log.setCustomer(customer);
        log.setOperator(operator);
        log.setOperationType(CustomerLogOperationType.STATUS_CHANGE);
        log.setOperationTime(System.currentTimeMillis());
        log.setOperationDesc("修改状态");
        log.setStatusBefore(statusBefore);
        log.setStatusAfter(statusAfter);
        return customerLogRepository.save(log);
    }
    
    @Override
    public CustomerLog logApprove(Customer customer, User operator, String comment) {
        CustomerLog log = new CustomerLog();
        log.setCustomer(customer);
        log.setOperator(operator);
        log.setOperationType(CustomerLogOperationType.APPROVE);
        log.setOperationTime(System.currentTimeMillis());
        log.setOperationDesc("审批通过");
        log.setStatusBefore("PENDING");
        log.setStatusAfter("APPROVED");
        log.setDetails(comment);
        return customerLogRepository.save(log);
    }
    
    @Override
    public CustomerLog logReject(Customer customer, User operator, String comment) {
        CustomerLog log = new CustomerLog();
        log.setCustomer(customer);
        log.setOperator(operator);
        log.setOperationType(CustomerLogOperationType.REJECT);
        log.setOperationTime(System.currentTimeMillis());
        log.setOperationDesc("审批拒绝");
        log.setStatusBefore("PENDING");
        log.setStatusAfter("INACTIVE");
        log.setDetails(comment);
        return customerLogRepository.save(log);
    }
    
    @Override
    public CustomerLog logDepartmentAdd(Customer customer, User operator, Department department, User sales) {
        CustomerLog log = new CustomerLog();
        log.setCustomer(customer);
        log.setOperator(operator);
        log.setOperationType(CustomerLogOperationType.DEPARTMENT_ADD);
        log.setOperationTime(System.currentTimeMillis());
        log.setOperationDesc("添加部门关联: " + department.getName() + "，销售人员: " + sales.getUsername());
        return customerLogRepository.save(log);
    }
    
    @Override
    public CustomerLog logDepartmentRemove(Customer customer, User operator, Department department) {
        CustomerLog log = new CustomerLog();
        log.setCustomer(customer);
        log.setOperator(operator);
        log.setOperationType(CustomerLogOperationType.DEPARTMENT_REMOVE);
        log.setOperationTime(System.currentTimeMillis());
        log.setOperationDesc("移除部门关联: " + department.getName());
        return customerLogRepository.save(log);
    }
    
    @Override
    public CustomerLog logContactAdd(Customer customer, User operator, Contact contact) {
        CustomerLog log = new CustomerLog();
        log.setCustomer(customer);
        log.setOperator(operator);
        log.setOperationType(CustomerLogOperationType.CONTACT_ADD);
        log.setOperationTime(System.currentTimeMillis());
        log.setOperationDesc("添加联系人: " + contact.getName());
        return customerLogRepository.save(log);
    }
    
    @Override
    public CustomerLog logContactUpdate(Customer customer, User operator, Contact contact, String details) {
        CustomerLog log = new CustomerLog();
        log.setCustomer(customer);
        log.setOperator(operator);
        log.setOperationType(CustomerLogOperationType.CONTACT_UPDATE);
        log.setOperationTime(System.currentTimeMillis());
        log.setOperationDesc("更新联系人: " + contact.getName());
        log.setDetails(details);
        return customerLogRepository.save(log);
    }
    
    @Override
    public CustomerLog logContactDelete(Customer customer, User operator, Contact contact) {
        CustomerLog log = new CustomerLog();
        log.setCustomer(customer);
        log.setOperator(operator);
        log.setOperationType(CustomerLogOperationType.CONTACT_DELETE);
        log.setOperationTime(System.currentTimeMillis());
        log.setOperationDesc("删除联系人: " + contact.getName());
        return customerLogRepository.save(log);
    }
    
    @Override
    public CustomerLog logDelete(Customer customer, User operator) {
        CustomerLog log = new CustomerLog();
        log.setCustomer(customer);
        log.setOperator(operator);
        log.setOperationType(CustomerLogOperationType.DELETE);
        log.setOperationTime(System.currentTimeMillis());
        log.setOperationDesc("删除客户");
        return customerLogRepository.save(log);
    }
    
    @Override
    public List<CustomerLogDTO> getLogsByCustomerId(Long customerId) {
        return customerLogRepository.findByCustomerIdOrderByOperationTimeDesc(customerId)
                .stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    @Override
    public List<CustomerLogDTO> getLogsByOperator(Long operatorId) {
        return customerLogRepository.findByOperatorId(operatorId)
                .stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    private CustomerLogDTO convertToDTO(CustomerLog log) {
        CustomerLogDTO dto = new CustomerLogDTO();
        BeanUtils.copyProperties(log, dto);
        dto.setCustomerId(log.getCustomer().getId());
        dto.setOperatorId(log.getOperator().getId());
        dto.setOperatorName(log.getOperator().getUsername());
        dto.setOperationTypeLabel(log.getOperationType().getLabel());
        dto.setStatusBefore(log.getStatusBefore());
        dto.setStatusAfter(log.getStatusAfter());
        return dto;
    }
} 